package com.atguigu.lease.web.admin.controller.lease;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Tag(name = "租约管理")
@RestController
@RequestMapping("/admin/agreement")
public class LeaseAgreementController {
    //注入service
    @Autowired
    private LeaseAgreementService agreementService;

    @Operation(summary = "保存或修改租约信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        //调用server实现
        boolean b = agreementService.saveOrUpdate(leaseAgreement);
        //判断成功
        if (!b) {
            return Result.fail();
        }
        //成功返回
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询租约列表")
    @GetMapping("page")
    public Result<IPage<AgreementVo>> page(@RequestParam long current, @RequestParam long size, AgreementQueryVo queryVo) {
        Page<AgreementVo> page = new Page<>(current,size);
        agreementService.customPage(page,queryVo);
        return Result.ok(page);
    }

    @Operation(summary = "根据id查询租约信息")
    @GetMapping(name = "getById")
    public Result<AgreementVo> getById(@RequestParam Long id) {
        //自定义一个service
        AgreementVo agreementVo = agreementService.customSelectById(id);
        return Result.ok(agreementVo);
    }

    @Operation(summary = "根据id删除租约信息")
    @DeleteMapping("removeById")
    public Result removeById(@RequestParam Long id) {
        //单表查询直接写方法就行
        //定义一个Lambda用来动态查询
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //找出要删除的ID
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getId,id);
        //不删除已签约的
        leaseAgreementLambdaQueryWrapper.notIn
                (LeaseAgreement::getStatus,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING,LeaseStatus.RENEWING);
        //判断是否删除成功
        boolean remove = agreementService.remove(leaseAgreementLambdaQueryWrapper);
        if (!remove) {
            return Result.fail();
        }
        return Result.ok();
    }

    @Operation(summary = "根据id更新租约状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus status) {
        //单表修改直接定义LambdaUpdateWapper可以修改和装值
        LambdaUpdateWrapper<LeaseAgreement> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        //找出要修改的Id
        lambdaUpdateWrapper.eq(LeaseAgreement::getId,id);
        //修改状态
        lambdaUpdateWrapper.set(LeaseAgreement::getStatus,status);
        //传给service完成修改
        agreementService.update(lambdaUpdateWrapper);
        return Result.ok();
    }

}

